﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class MaterialCategoryService : BaseService, IMaterialCategoryService
    {
        private readonly IMaterialCategoryRepository _materialCategoryRepository;
        private readonly IUnitOfWork _unitOfWork;
        public MaterialCategoryService(IMapper mapper, IMaterialCategoryRepository _materialCategoryRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._materialCategoryRepository = _materialCategoryRepository;
            this._unitOfWork = _unitOfWork; 
        }

        public bool AddMaterialCategory(MaterialCategory m, out string message)
        {
            message = "";
            var obj = _materialCategoryRepository.QueryIsEntityWhere(x => x.Code == m.Code);
            if (obj != null)
            {
                message = "编码已存在！";
                return false;
            }
            _materialCategoryRepository.AddEntity(m);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelMaterialCategory(long mid)
        {
            var obj = _materialCategoryRepository.QueryEntityById(mid);
            if (obj != null)
            {
                _materialCategoryRepository.DelEntity(obj);
                var objparentidlist =_materialCategoryRepository.GetEntityWhereAll(x => x.ParentId == mid);
                if (objparentidlist.Any())
                    _materialCategoryRepository.DelEntityList(objparentidlist.ToList());
            }
            return _unitOfWork.SaveChangesEntity();
        }

        public List<MenuDto> GetMaterialCategortList(string? mname, int pageIndex, int pageSize, out int totalCount)
        {
            var mlist = _materialCategoryRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(mname))
            {
                mlist = mlist.Where(x => x.Name.Contains(mname));
            }

            if (mlist.Any())
            {
                mlist = mlist.OrderBy(x => x.ParentId);
                var mmlist = mlist.FirstOrDefault();
                var list2 = GetMenuLists(mlist.ToList(), mmlist.ParentId);
                totalCount = list2.Count();
                list2 = list2.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                return list2;
            }

            var list = GetMenuLists(mlist.ToList(), 0);
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return list;
        }
        /// <summary>
        /// 递归显示分类列表
        /// </summary>
        /// <param name="list"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        private static List<MenuDto> GetMenuLists(List<MaterialCategory> list, long? pid)
        {
            try
            {
                return list.Where(x => x.ParentId == pid).Select(x => new MenuDto
                {
                    id = x.Id,
                    ParentId = x.ParentId,      
                    Meta = new MetaDto
                    {
                        MenuName = x.Name,
                        MenuIcon = x.Code,
                    },
                    DisplayOrder = x.DisplayOrder,
                    children = GetMenuLists(list, x.Id)
                }).OrderBy(x => x.DisplayOrder).ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public MaterialCategory? GetMaterialCategoryById(long mid)
        {
            return _materialCategoryRepository.QueryEntityById(mid);
        }

        public List<Menuitem> GetMaterialCateMenuitemList()
        {
            var list = _materialCategoryRepository.GetEntityAlls();

            return GetMaterialCateMenuitemList(list.ToList(), 0);
        }
        private static List<Menuitem> GetMaterialCateMenuitemList(List<MaterialCategory> list, long? pid)
        {
            return list.Where(x => x.ParentId == pid).Select(x => new Menuitem
            {
                value = x.Id,
                label = x.Name,
                children = GetMaterialCateMenuitemList(list, x.Id).Count > 0 ? GetMaterialCateMenuitemList(list, x.Id) : null
            }).ToList();
        }

        public bool UpdMaterialCategory(MaterialCategory m, out string message)
        {
            message = "";
            var list = _materialCategoryRepository.QueryIsEntityWhere(x => x.Id != m.Id && x.Code == m.Code);

            if (list != null)
            {
                message = "编码已存在！";
                return false;
            }
            _materialCategoryRepository.UpdEntity(m);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
